﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSWord=Microsoft.Office.Interop.Word ;
using System.Reflection;
using BrokerBase.Lib.Policy;
using BrokerBase.Lib.Person;


namespace BrokerBase.Lib.Tool
{
    public static class DocUtil
    {

        public static void CreateDoc(IScheme scheme)
        {


            MSWord.Application wordApp = new Microsoft.Office.Interop.Word.Application();

            MSWord.Document doc = wordApp.Documents.Add();

            SetHeadAndFoot(scheme, wordApp);

            wordApp.Selection.ParagraphFormat.LineSpacing = 20f; // 设置文档的行间距  

            NewLine(wordApp);
            
            doc.Paragraphs.Last.Range.Font.Size = 12;

            string s1 = "尊敬的" + scheme.Insurant.Name + (scheme.Insurant.Sex == SexType.Male ? "先生" : "女士")+":";

            doc.Paragraphs.Last.Range.Text = s1;

            NewLine(wordApp);

            wordApp.Visible = true;
        }
        /// <summary>
        /// 设置页眉和页脚
        /// </summary>
        /// <param name="scheme"></param>
        /// <param name="wordApp"></param>
        private static void SetHeadAndFoot(IScheme scheme, MSWord.Application wordApp)
        {
            wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdOutlineView;
            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryHeader;
            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter("美联达");
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐   

            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekCurrentPageFooter;

            string company = scheme.Policies[0].Product.Company.FullName;

            wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(company);
            wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight; // 设置右对齐   

            wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument; // 跳出页眉设置   

            
        }

        private static void WriteCompanyDescription(IScheme scheme, MSWord.Application wordApp)
        {
            
        }

        private static void NewLine(MSWord.Application wordApp)
        {
            object nothing = Missing.Value;
            // 移动焦点并换行   
            object count = 14;
            object WdLine = MSWord.WdUnits.wdLine; // 换一行;   
            wordApp.Selection.MoveDown(ref  WdLine, ref  count, ref nothing); // 移动焦点   
        }

        private static void SetGrid()
        {

        }
    }
}
